﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class TRINHDODAO:IData<TRINHDO> 
 	{
		public TRINHDODAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref TRINHDO item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + TRINHDO.c_ID;
			 p.Value = item.ID;
			 Object obj = item.ID; 
			int i = connec.ExecuteNonQuery("sp_TRINHDO", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + TRINHDO.c_MATD, item.MATD), new SqlParameter("@" + TRINHDO.c_TENTD, item.TENTD));
			if(t == 0)
				item.ID = Convert.ToInt32(obj);
			return i;
		}

		public override TRINHDO GetItem(IDataReader dr)
		{
			TRINHDO obj = new TRINHDO();
			obj.ID = Convert.ToInt32(dr[TRINHDO.c_ID]);
			obj.MATD = Convert.ToString(dr[TRINHDO.c_MATD]);
			obj.TENTD = Convert.ToString(dr[TRINHDO.c_TENTD]);
			return obj;
		}

		public override List<TRINHDO> GetAll()
		{
			return GetAll("sp_GetAllTRINHDO", CommandType.StoredProcedure);
		}

		public override TRINHDO GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.ID == Convert.ToInt32(id));
		}
		public override TRINHDO GetItemById(TRINHDO item){
			throw new NotImplementedException();
		}

	}

 }
